/**
 * @(#) DeputyMediator.as
 *
 * @date		2010. 09. 08.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @client		Network O&S 
 * @description	대결지정 Mediator (결재함)
 * 				approval.DeputyMediator에서 복사..
 */
package com.skt.common_hr.approval_nos.view
{
	import com.skt.common_hr.approval_nos.model.DeputyProxy;
	import com.skt.common_hr.approval_nos.model.vo.DeputyVO;
	import com.skt.common_hr.approval_nos.view.components.DeputyView;
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.memberselector.MemberSelectorMediator;
	import com.skt.common_hr.common.puremvc.CommonMediator;
	import com.wcl.utils.StringUtil;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;

	public class DeputyMediator extends CommonMediator
	{
		public static const NAME:String = "DeputyMediator";
		public static const CLOSED_DEPUTY_VIEW:String = "closedDeputyView";
		public static const CANCEL_DEPUTY_VIEW:String = "cancelDeputyView";
		
		private var bWaitingDelegator:Boolean = false;
			
		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
		public function DeputyMediator()
        {
            super( NAME, null );
        }
		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
		override protected function initializeView():void 
 		{
 			setEventListener();
 		}

		private function setEventListener():void
		{
			view.addEventListener(DeputyView.SAVE_DEPUTY, onSaveDeputy);
			view.addEventListener(DeputyView.CLOSE_VIEW, onCloseView);
			view.addEventListener(DeputyView.SEARCH_MEMBER, onSearchMember);
		}




		public function execView(parent:DisplayObject):void
		{
			if(view==null) {
				createPopUp(parent);
			}	
			else {
				PopUpManager.addPopUp(view, parent.root,true);
			}		
			view.setFocus();
			bActivated = true;
			view.initView();
			setEventListener();
		}
		
		public function execInfo(parent:DisplayObject, info:DeputyVO):void
		{
			if(view==null) {
				createPopUp(parent);
			}	
			else {
				PopUpManager.addPopUp(view, parent.root,true);
			}		
			view.setFocus();
			bActivated = true;
			//view.initView();
			view.initInfoView(info);
			setEventListener();
		}
		
		private function loadApprovalDetail(key:String):void
		{
			var proxy:DeputyProxy = facade.retrieveProxy(DeputyProxy.NAME) as DeputyProxy;
			proxy.loadDeputy(Variables.myPernr);
		}


		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return [
					DeputyProxy.SAVE_DEPUTY_COMPLETE,
					DeputyProxy.SAVE_DEPUTY_FAILED,
					MemberSelectorMediator.SELECTED_MEMBER,
					MemberSelectorMediator.CANCEL_SELECTION
					];
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			switch(note.getName()) 
			{
				case DeputyProxy.SAVE_DEPUTY_COMPLETE:
					view.closeView();
					facade.sendNotification(CLOSED_DEPUTY_VIEW);
					break;
				case DeputyProxy.SAVE_DEPUTY_FAILED:
					trace("failed to save deputy");
					break;
				// 구성원 선택  대화상자에서 구성원 선택
				case MemberSelectorMediator.SELECTED_MEMBER:
					if(bWaitingDelegator) {
						//execDelegation(MemberSelectorMediator(note.getBody()).selectedMember.PERNR);
						view.dataProvider.CNAME = MemberSelectorMediator(note.getBody()).selectedMember.ENAME;
						view.dataProvider.CPERNR= MemberSelectorMediator(note.getBody()).selectedMember.PERNR;
					}
					bWaitingDelegator = false;
					break;
				// 구성원 선택  대화상자에서 구성원 취소
				case MemberSelectorMediator.CANCEL_SELECTION:
					break;
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}



		private function createPopUp(parent:DisplayObject):void
		{
			viewComponent = PopUpManager.createPopUp(parent.root, DeputyView, true);
			PopUpManager.centerPopUp(view);
			view.setFocus();
		}
		
		
		
		private function onCloseView(e:Event):void
		{
			view.closeView();
			facade.sendNotification(CANCEL_DEPUTY_VIEW, this);
		}
		
		private function onSaveDeputy(e:Event):void
		{
			if(validData()) {
				saveData();
			}
			else {
				trace("invalid data");
			}
		}
		
		private function onSearchMember(e:Event):void
		{
			selectDelegator();
		}
		
		private function validData():Boolean
		{
			if(view.dataProvider.BEGDA.valueOf() > view.dataProvider.ENDDA.valueOf()) {
				CommonAlert.showMessage("", "부재기간을 정확하게 입력하시기 바랍니다.", null, "확인"); 
				return false;
			}
			if(StringUtil.NVL(view.dataProvider.CPERNR) == "") {
				CommonAlert.showMessage("", "검색버튼을 누르시고 대결자를 선택하시기 바랍니다.", null, "확인"); 
				return false;
			}
			if(StringUtil.NVL(view.dataProvider.TITLE) == "") {
				CommonAlert.showMessage("", "부재사유를 정확하게 입력하시기 바랍니다.", null, "확인"); 
				return false;
			}
			return true;
		}

		private function saveData():void
		{
			var proxy:DeputyProxy = facade.retrieveProxy(DeputyProxy.NAME) as DeputyProxy;
			proxy.saveDeputy(Variables.myPernr, view.dataProvider);
		}

		private function selectDelegator():void
		{
			var memberSelector:MemberSelectorMediator = 
					facade.retrieveMediator(MemberSelectorMediator.NAME) as MemberSelectorMediator;
			if(memberSelector==null) {
				memberSelector = new MemberSelectorMediator();
				facade.registerMediator(memberSelector);
			}
			memberSelector.exec(view);
			bWaitingDelegator = true;
			/*
			
			*/
		}		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():DeputyView
        {
            return viewComponent as DeputyView;
        }
	}
}